﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Resources;
using Cesave.Domain.Core;
using System.Data;

namespace Cesave.Domain.PermisosModule.Contracts.CultivosAguas
{
	/// <summary>
	/// 
	/// </summary>
	public class CultivosAguaService
		: ICultivosAguaService
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private ICultivosAguaRepository _cultivosAguaRepository;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cultivosAguaRepository"></param>
		public CultivosAguaService(ICultivosAguaRepository cultivosAguaRepository)
		{
			if (cultivosAguaRepository == null)
				throw new ArgumentNullException(Messages.exception_DependenciesAreNotInitialized, "cultivosAguaRepository");
			_cultivosAguaRepository = cultivosAguaRepository;
		}

		#endregion

		#region ICultivosAguaService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cultivosAgua"></param>
		public void AddCultivosAgua(Entities.CultivosAgua cultivosAgua)
		{
			if (cultivosAgua == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "cultivosAgua");
			IUnitOfWork unitOfWork = _cultivosAguaRepository.StoreContext;
			_cultivosAguaRepository.Add(cultivosAgua);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cultivosAgua"></param>
		public void ChangeCultivosAgua(Entities.CultivosAgua cultivosAgua)
		{
			if (cultivosAgua == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "cultivosAgua");
			IUnitOfWork unitOfWork = _cultivosAguaRepository.StoreContext;
			_cultivosAguaRepository.Modify(cultivosAgua);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cultivoGuid"></param>
		/// <param name="cicloGuid"></param>
		/// <param name="juntaGuid"></param>
		/// <returns></returns>
		public List<Entities.CultivosAgua> FindCultivosAgua(Guid cultivoGuid, Guid cicloGuid, Guid juntaGuid)
		{
			if (cultivoGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "cultivoGuid");
			if (cicloGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "cicloGuid");
			if (juntaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "juntaGuid");
			return _cultivosAguaRepository.FindCultivosAguas(
				new CultivosAguasByCultivoSpecification(cultivoGuid),
				new CultivosAguasByCicloSpecification(cicloGuid),
				new CultivosAguasByJuntaSpecification(juntaGuid)
			).ToList();
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			if (_cultivosAguaRepository != null && _cultivosAguaRepository.StoreContext != null)
				_cultivosAguaRepository.StoreContext.Dispose();
		}

		#endregion
	}
}
